<template>
  <div class="dashboard-editor-container">
    <!-- <panel-group @handleSetLineChartData="handleSetLineChartData" />

    <el-row style="background: #fff; padding: 16px 16px 0; margin-bottom: 32px">
      <line-chart :chart-data="lineChartData" />
    </el-row>

    <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart />
        </div>
      </el-col>
    </el-row> -->
    <div class="index-top">
      <div class="index-top-1">
        <div class="index-top-1-li">
          <div class="index-top-1-li-left" style="background: #2d8cf0">
            <img src="@/assets/image/用户组.png" alt="" />
          </div>
          <div class="index-top-1-li-right">
            <span class="num" style="color: #2d8cf0">227,543</span>
            <span class="title">总用户数</span>
          </div>
        </div>
        <div class="index-top-1-li">
          <div class="index-top-1-li-left" style="background: #19be6b">
            <img src="@/assets/image/新增用户.png" alt="" />
          </div>
          <div class="index-top-1-li-right">
            <span class="num" style="color: #19be6b">3</span>
            <span class="title">今日新增用户</span>
          </div>
        </div>
        <div class="index-top-1-li">
          <div class="index-top-1-li-left" style="background: #ff9900">
            <img src="@/assets/image/登录.png" alt="" />
          </div>
          <div class="index-top-1-li-right">
            <span class="num" style="color: #ff9900">9</span>
            <span class="title">今日登录用户</span>
          </div>
        </div>
      </div>
      <div class="index-top-2">
        <PieChart1 text="用户类型" :dataPropos="data1" />
      </div>
      <div class="index-top-2">
        <PieChart1 text="用户来源" :dataPropos="data2" />
      </div>
    </div>
    <!-- 下边 -->
    <div class="index-top">
      <div class="index-top-3">
        <div class="index-top-3-top">
          <div
            :class="
              indexActive == index
                ? 'index-top-3-top-li active'
                : 'index-top-3-top-li'
            "
            v-for="(item, index) in dataTime"
            @click="indexChange(item, index)"
          >
            {{ item }}
          </div>
          <el-date-picker
            style="height: 35px; margin-left: 30px; width: 250px"
            v-model="value"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <div class="index-top-3-bottom">
          <line-chart
            style="margin-right: 30px"
            text="用户数及活跃度趋势图"
            height="240px"
            :dataLegend="dataLegend"
            :dataAxis="dataAxis"
            :series="series"
          />
        </div>
      </div>
      <div class="index-top-2">
        <PieChart1 text="用户性别" :dataPropos="data3" />
      </div>
    </div>
  </div>
</template>

<script>
import PanelGroup from "./dashboard/PanelGroup";
import LineChart from "./dashboard/LineChart";
import RaddarChart from "./dashboard/RaddarChart";
import PieChart from "./dashboard/PieChart";
import PieChart1 from "./dashboard/PieChart1";
import BarChart from "./dashboard/BarChart";
import { getToken, getExpiresIn, setExpiresIn } from "@/utils/auth";

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145],
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130],
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130],
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130],
  },
};

export default {
  name: "Index",
  components: {
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    PieChart1,
    BarChart,
  },
  data() {
    return {
      //刷新token锁
      refreshLock: false,
      //刷新token的时间
      refreshTime: "",
      value: "",
      lineChartData: lineChartData.newVisitis,
      indexActive: 0,
      dataTime: ["近一月", "近三月", "近半年", "近一年"],
      data1: [
        { value: 320, name: "管理员" },
        { value: 240, name: "运营人员" },
        { value: 149, name: "财务" },
        { value: 100, name: "供应商" },
        { value: 59, name: "分销商" },
        { value: 59, name: "子分销商" },
        { value: 59, name: "旅客" },
      ],
      data2: [
        { value: 435, name: "添加注册号" },
        { value: 23, name: "微信公众号" },
        { value: 14, name: "便捷青岛" },
        { value: 345, name: "微信小程序" },
        { value: 2342, name: "支付宝" },
      ],
      data3: [
        { value: 187, name: "男" },
        { value: 167, name: "女" },
        { value: 14, name: "未知" },
      ],
      dataLegend: ["新增用户数", "登录用户数", "总用户数"],
      dataAxis: [
        "2024-03-27",
        "2024-02-27",
        "2024-01-27",
        "2023-12-27",
        "2023-11-27",
        "2023-10-27",
        // "2023-09-27",
      ],
      series: [
        {
          name: "新增用户数",
          itemStyle: {
            normal: {
              color: "#FF005A",
              lineStyle: {
                color: "#FF005A",
                width: 2,
              },
            },
          },
          smooth: true,
          type: "line",
          data: [120, 132, 101, 134, 90, 230, 210],
          animationDuration: 2800,
          animationEasing: "cubicInOut",
        },
        {
          name: "登录用户数",
          smooth: true,
          type: "line",
          itemStyle: {
            normal: {
              color: "#3888fa",
              lineStyle: {
                color: "#3888fa",
                width: 2,
              },
              areaStyle: {
                color: "#f3f8ff",
              },
            },
          },
          data: [220, 182, 191, 234, 290, 330, 310],
          animationDuration: 2800,
          animationEasing: "quadraticOut",
        },
        {
          name: "总用户数",
          smooth: true,
          type: "line",
          itemStyle: {
            normal: {
              color: "#19BE6B",
              lineStyle: {
                color: "#19BE6B",
                width: 2,
              },
              areaStyle: {
                color: "#f3f8ff",
              },
            },
          },
          data: [150, 232, 201, 154, 190, 330, 410],
          animationDuration: 2800,
          animationEasing: "quadraticOut",
        },
      ],
    };
  },
  created() {
    this.refreshToken();
  },
  methods: {
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type];
    },
    // 实时检测刷新token
    refreshToken() {
      this.refreshTime = setInterval(() => {
        if (null === getToken()) {
          return;
        }
        const expires_in = getExpiresIn();
        if (expires_in <= 1000 && !this.refreshLock) {
          this.refreshLock = true;
          this.$store.dispatch("RefreshToken").catch(() => {
            clearInterval(this.refreshTime);
          });
          this.refreshLock = false;
        }
        this.$store.commit("SET_EXPIRES_IN", expires_in - 10);
        setExpiresIn(expires_in - 10);
      }, 10000);
    },
    indexChange(item, index) {
      this.indexActive = index;
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 32px;
  // background-color: rgb(240, 242, 245);
  min-height: 100vh;
  background: #f2f8ff;
  position: relative;

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
  .index-top {
    display: flex;
    align-content: center;
    justify-content: space-between;
    height: 44.5vh;
    div {
      height: 100%;
    }
    .index-top-1 {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      width: 19%;
      .index-top-1-li {
        display: flex;
        align-items: center;
        height: 31%;
        background: #fff;
        border-radius: 5px;
        .index-top-1-li-left {
          display: flex;
          justify-content: center;
          align-content: center;
          width: 30%;
          border-top-left-radius: 5px;
          border-bottom-left-radius: 5px;
          img {
            width: 40px;
            object-fit: contain;
          }
        }
        .index-top-1-li-right {
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          width: 70%;
          border-top-right-radius: 5px;
          border-bottom-right-radius: 5px;
          .num {
            font-size: 30px;
            font-weight: bold;
          }
          .title {
            margin-top: 6px;
            font-size: 13px;
          }
        }
      }
    }
    .index-top-2 {
      padding-top: 2%;
      width: 39.5%;
      background: #fff;
    }
    .index-top-3 {
      padding: 15px;
      width: 59.5%;
      background: #fff;
      .index-top-3-top {
        display: flex;
        justify-content: right;
        height: 35px;
        .index-top-3-top-li {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 80px;
          height: 35px;
          border: 1px solid #999;
          color: #999;
          font-size: 14px;
          cursor: pointer;
        }
        .active {
          border: 1px solid #2d8cf0 !important;
          color: #2d8cf0 !important;
        }
      }
      .index-top-3-bottom {
        margin-top: 20px;
      }
    }
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
